package com.ruoyi.web.controller.report;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.report.domain.ReportDataSource;
import com.ruoyi.report.service.IReportDataSourceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据源管理Controller
 * 
 * @author sunyan
 * @date 2022-03-24
 */
@RestController
@RequestMapping("/report/datasource")
public class ReportDataSourceController extends BaseController
{
    @Autowired
    private IReportDataSourceService reportDataSourceService;

    /**
     * 查询数据源管理列表
     */
    @PreAuthorize("@ss.hasPermi('report:datasource:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReportDataSource reportDataSource)
    {
        startPage();
        String username = SecurityUtils.getUsername();
        //管理员用户查询全部，其他账户只查询自己的数据库链接地址
        if(!username.equals("admin")) {
            reportDataSource.setCreateBy(username);
        }
        List<ReportDataSource> list = reportDataSourceService.selectReportDataSourceList(reportDataSource);
        return getDataTable(list);
    }

    /**
     * 导出数据源管理列表
     */
    @PreAuthorize("@ss.hasPermi('report:datasource:export')")
    @Log(title = "数据源管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReportDataSource reportDataSource)
    {
        List<ReportDataSource> list = reportDataSourceService.selectReportDataSourceList(reportDataSource);
        ExcelUtil<ReportDataSource> util = new ExcelUtil<ReportDataSource>(ReportDataSource.class);
        util.exportExcel(response, list, "数据源管理数据");
    }

    /**
     * 获取数据源管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('report:datasource:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(reportDataSourceService.selectReportDataSourceById(id));
    }

    /**
     * 新增数据源管理
     */
    @PreAuthorize("@ss.hasPermi('report:datasource:add')")
    @Log(title = "数据源管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReportDataSource reportDataSource)
    {
        reportDataSource.setCreateBy(SecurityUtils.getUsername());
        return toAjax(reportDataSourceService.insertReportDataSource(reportDataSource));
    }

    /**
     * 修改数据源管理
     */
    @PreAuthorize("@ss.hasPermi('report:datasource:edit')")
    @Log(title = "数据源管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReportDataSource reportDataSource)
    {
        reportDataSource.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(reportDataSourceService.updateReportDataSource(reportDataSource));
    }

    /**
     * 删除数据源管理
     */
    @PreAuthorize("@ss.hasPermi('report:datasource:remove')")
    @Log(title = "数据源管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(reportDataSourceService.deleteReportDataSourceByIds(ids));
    }
}
